package com.tjbklx1.rocketMQTest.demo03;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import com.tjbklx1.rocketMQTest.constant.Host;

import java.util.Date;

public class Producer {
	public static void main(String[] args) throws MQClientException {
		DefaultMQProducer producer = new DefaultMQProducer("rmq-group");
		/**
		 * 默认情况下，一台服务器只能启动一个Producer或Consumer实例，所以如果需要在 一台服务器启动多个实例，需要设置实例的名称。
		 */
		producer.setNamesrvAddr(Host.HOST);
		producer.setInstanceName("producer");
		producer.start();
		try {
			for (int i = 0; i < 10; i++) {
				Thread.sleep(1000);
				// 每秒发送一次MQ
				Message msg = new Message("TopicA-test", "TagA",
						(new Date() + "Hello RocketMQ,QuickStart" + i).getBytes());
				SendResult sendResult = producer.send(msg);
				System.out.println(sendResult);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		producer.shutdown();
	}
}
